package com.frxuexi.park2303.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.entity.NewsBeanItem
import com.frxuexi.base.entity.ParkBeanItem
import com.frxuexi.base.entity.StatusBean
import com.frxuexi.base.entity.VisitBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.park2303.R
import com.frxuexi.park2303.databinding.ActivityMyApproveBinding
import com.frxuexi.park2303.user.adapter.MyApproveAdapter
import com.frxuexi.visit.adapter.VisitStatusAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApproveActivity")
@AndroidEntryPoint
class MyApproveActivity : BaseActivity<ActivityMyApproveBinding,UserIntent,UserViewModel>() {
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val parkList= mutableListOf<ParkBeanItem>()
    val visitList= mutableListOf<VisitBeanItem>()
    val newsList= mutableListOf<NewsBeanItem>()
    val myApproveAdapter by lazy { MyApproveAdapter() }
    var position=-1
    override fun getVM(): UserViewModel {
        return viewModels<UserViewModel>().value
    }

    override fun getBind(): ActivityMyApproveBinding {
        return ActivityMyApproveBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<ParkBeanItem>
                                items.forEach {
                                    it.itemType=0
                                }
                                parkList.addAll(items)
                                myApproveAdapter.addData(parkList)
                            }
                            RequestType.UPDATE->{
                                myApproveAdapter.notifyItemChanged(position)
                            }
                            RequestType.VISIT->{
                                val items = it.data as Collection<VisitBeanItem>
                                items.forEach {
                                    it.itemType=1
                                }
                                visitList.addAll(items)
                                myApproveAdapter.addData(visitList)
                            }
                            RequestType.NEWS->{
                                val items = it.data as Collection<NewsBeanItem>
                                items.forEach {
                                    it.itemType=2
                                }
                                newsList.addAll(items)
                                myApproveAdapter.addData(newsList)
                            }
                            else -> {}
                        }
                        myApproveAdapter.notifyDataSetChanged()

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.headApprove.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("审核管理")
            headAdd.visibility=View.GONE
        }

        val statusBeans = listOf<StatusBean>(
            StatusBean("全部", true),
            StatusBean("车位审核", false),
            StatusBean("来访审核", false),
            StatusBean("新闻审核", false),
        )
        visitStatusAdapter.addData(statusBeans)
        binding.appRv.apply {
            adapter=visitStatusAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity,RecyclerView.HORIZONTAL,false)
        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck=false
            }
            visitStatusAdapter.data.get(position).isCheck=true
            visitStatusAdapter.notifyDataSetChanged()

            myApproveAdapter.apply {
                data.clear()
                when(position){
                    0->{
                        addData(parkList)
                        addData(visitList)
                        addData(newsList)
                    }
                    1->{
                        addData(parkList)
                    }
                    2->{
                        addData(visitList)
                    }
                    3->{
                        addData(newsList)
                    }
                }
                notifyDataSetChanged()
            }

        }

        binding.rvApprove.apply {
            adapter=myApproveAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity)
        }

        vm.sendIntent(UserIntent.getParkByType)
        vm.sendIntent(UserIntent.getVisits)
        vm.sendIntent(UserIntent.getNews)

        myApproveAdapter.addChildClickViewIds(com.frxuexi.park.R.id.btn_ok, com.frxuexi.park.R.id.btn_no,com.frxuexi.visit.R.id.vistit_ok,com.frxuexi.visit.R.id.visit_no)
        myApproveAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = myApproveAdapter.data.get(position)
            this.position=position
            when(view.id){
                com.frxuexi.park.R.id.btn_ok->{
                    val beanItem = bean as ParkBeanItem
                    beanItem.spaceStatus="维护中"
                    vm.sendIntent(UserIntent.updatePark(beanItem.id!!,beanItem))
                }
                com.frxuexi.park.R.id.btn_no->{
                    val beanItem = bean as ParkBeanItem
                    beanItem.spaceStatus="已占用"
                    vm.sendIntent(UserIntent.updatePark(beanItem.id!!,beanItem))
                }
                com.frxuexi.visit.R.id.vistit_ok->{
                    val beanItem = bean as VisitBeanItem
                    vm.sendIntent(UserIntent.approveVisit(beanItem.id!!))
                    beanItem.visitStatus="已通过"
                }
                com.frxuexi.visit.R.id.visit_no->{
                    val beanItem = bean as VisitBeanItem
                    vm.sendIntent(UserIntent.rejectVisit(beanItem.id!!))
                    beanItem.visitStatus="驳回"
                }
            }
        }
    }

}